
import sys, os
from os import system as Exec


tmpl_test_new = r'''
#include <iostream>
using namespace std;

#include "gtest/gtest.h"

#include "%(cls)s.h"


TEST( %(cls)sTest, Simple )
{
    FAIL();
}

#include "main_stub.h"

'''

tmpl_prog_new = r'''
#include <iostream>
using namespace std;

#include "ProgCommon.h"


int main()
{
    PROG_BANNER;

    ConSetColor( FG_CYAN );
    printf( "Prog %(cls)s\n" );
    ConSetColor( COLOR_DEFAULT );

    printf( "\n" );
    PROG_BANNER;
    return 0;
}

'''


def action_all_test( argv ):
    from glob import glob
    fnames = glob( 'test/test_*.cpp' )

    fobj = file( 'test/AllTest.cpp', 'w+' )
    for fn in fnames:
        fobj.write( '#include "%s"\n' % fn[5:] )
    fobj.close()

def action_test_new( argv ):
    if not len(argv) > 0:
        return

    cls_name = argv[0]
    fname = 'test/test_%s.cpp' % cls_name

    if os.path.exists( fname ):
        cho = raw_input( '\n  %s has exists, override? [y/n]: ' % fname )
        if str(cho) != 'y':
            return

    fobj = file( fname, 'w+' )
    dct = {}
    dct['cls'] = cls_name
    fobj.write( tmpl_test_new % dct )
    fobj.close()

def action_test_run( argv ):
    if not len(argv) > 0:
        return

    fname = '.\\bin\\%s_test.exe' % argv[0]
    Exec( '%s' % fname )


def action_prog_new( argv ):
    if not len(argv) > 0:
        return

    cls_name = argv[0]
    fname = 'prog/prog_%s.cpp' % cls_name

    if os.path.exists( fname ):
        cho = raw_input( '\n  %s has exists, override? [y/n]: ' % fname )
        if str(cho) != 'y':
            return

    fobj = file( fname, 'w+' )
    dct = {}
    dct['cls'] = cls_name
    fobj.write( tmpl_prog_new % dct )
    fobj.close()

def action_prog_run( argv ):
    if not len(argv) > 0:
        return

    fname = '.\\bin\\%s_prog.exe' % argv[0]
    Exec( '%s' % fname )

if __name__ == '__main__':
    argv = sys.argv[1:]
    argc = len(argv)
    G = globals()

    if argc >= 1:
        action = argv[0]
        func_action_s = 'action_%s' % action
        if func_action_s in G:
            func = G[func_action_s]
            func( argv[1:] )
            print ' * Do Action %s' % action
        else:
            print ' * Action %s not found' % action
    else:
        print ' * Has no action to do'
        lst = [ str(e) for e in G.keys() if 'action_' in str(e) ]
        for e in lst:
            print ' - Action %s' % e[7:]

